package models

import "github.com/cy77cc/hioshop/global"

type Admin struct {
	Model
	Username      string `json:"username" gorm:"varchar(25);default:''"`
	Password      string `json:"password" gorm:"varchar(255);default:''"`
	PasswordSalt  string `json:"password_salt" gorm:"varchar(255);default:''"`
	LastLoginIp   string `json:"last_login_ip" gorm:"varchar(60);default:''"`
	LastLoginTime int    `json:"last_login_time" gorm:"int;default:0"`
	IsDelete      int8   `json:"is_delete" gorm:"tinyint;default:0"`
}

func (a *Admin) Create() error {
	return global.DB.Model(&Admin{}).Create(a).Error
}

func (a *Admin) GetAdminByName() error {
	return global.DB.Model(&Admin{}).Where("username=?", a.Username).Find(&a).Error
}

func (a *Admin) UpdateLoginInfo() error {
	return global.DB.Model(&Admin{}).Where("id=?", a.ID).Updates(map[string]any{
		"last_login_time": a.LastLoginTime,
		"last_login_ip":   a.LastLoginIp,
	}).Error
}
